package com.example.yichuangyun.mvvm.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemFileBinding;
import com.example.yichuangyun.http.ApiFactory;
import com.example.yichuangyun.model.order.AttachmentModel;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.pictrue.GlideEngine;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnExternalPreviewEventListener;

import java.util.ArrayList;

public class FileNameAdapter extends BaseQuickAdapter<AttachmentModel, DataBindingHolder<ItemFileBinding>> {
    private final Context context;
    private final ArrayList<LocalMedia> imgData = new ArrayList<>();

    public FileNameAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemFileBinding> itemFileBindingDataBindingHolder, int i, @Nullable AttachmentModel attachmentModel) {
        ItemFileBinding binding = itemFileBindingDataBindingHolder.getBinding();
        binding.setAttachmentModel(attachmentModel);
        if (attachmentModel != null) {
            binding.tvFileName.setOnClickListener(view -> {
                if (!TextUtils.isEmpty(attachmentModel.getAttachmentPath())) {
                    if (attachmentModel.getAttachmentPath().startsWith("http")) {
                        Uri uri = Uri.parse(attachmentModel.getAttachmentPath());
                        Intent intent = new Intent();
                        intent.setAction("android.intent.action.VIEW");
                        intent.setData(uri);
                        context.startActivity(intent);
                    } else {
                        if (CommonUtil.isImage(attachmentModel.getAttachmentPath())) {
                            imgData.clear();
                            LocalMedia localMedia = new LocalMedia();
                            localMedia.setPath(ApiFactory.FILE_URL + attachmentModel.getAttachmentPath());
                            imgData.add(localMedia);
                            PictureSelector.create(context)
                                    .openPreview()
                                    .setImageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
                                    .setExternalPreviewEventListener(new OnExternalPreviewEventListener() {
                                        @Override
                                        public void onPreviewDelete(int position) {

                                        }

                                        @Override
                                        public boolean onLongPressDownload(Context context, LocalMedia media) {  // 长按保存图片
                                            return false;
                                        }
                                    })
                                    .startActivityPreview(0, false, imgData);
                        } else {
                            Uri uri = Uri.parse(ApiFactory.FILE_URL + attachmentModel.getAttachmentPath());
                            Intent intent = new Intent();
                            intent.setAction("android.intent.action.VIEW");
                            intent.setData(uri);
                            context.startActivity(intent);
                        }
                    }
                }
            });
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemFileBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_file, viewGroup);
    }
}
